package com.unsiv.helicopter.entity;

import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.widget.Toast;

import com.unsiv.helicopter.Command;

/**
 * <p>
 * 油门控制器
 * </p>
 * 
 * @author fanzhen
 * @time 4:38:26 PM Oct 30, 2011
 * @email fanzhen.meng@gmail.com
 * @qq 474146502
 */
public class Accelerator extends AnimatedSprite {
	private Context context;
	public final static int HEIGHT = 350;
	public final static int WIDTH = 164;
	private final static int period = 35;
	private OnThrottleChangeListener onThrottleChangeListener;

	public Accelerator(Context context, float pX, float pY, float pTileWidth,
			float pTileHeight, TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion);
		Command.mThrottle = 0;
		if (null != onThrottleChangeListener)
			onThrottleChangeListener.onThrottleChange(Command.mThrottle);
		this.setCurrentTileIndex(9);
		this.context = context;
	}

	public Accelerator(Context context, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTiledTextureRegion);
		Command.mThrottle = 0;
		if (null != onThrottleChangeListener)
			onThrottleChangeListener.onThrottleChange(Command.mThrottle);
		this.setCurrentTileIndex(9);
		this.context = context;
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
				pTouchAreaLocalY);
		if(!Command.needDoCommand){
			Toast.makeText(context, "Sorry please insert a handset.",
					Toast.LENGTH_SHORT).show();
			return false;
		}
		AudioManager manager = (AudioManager) context
				.getSystemService(Activity.AUDIO_SERVICE);
		int maxv1 = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		int volume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
		if (maxv1 != volume) {
			Toast.makeText(context, "Sorry your device is not in max volume.",
					Toast.LENGTH_SHORT).show();
			return false;
		}
		
		if (Command.allowCommand) {
			switch (pSceneTouchEvent.getAction()) {
			case TouchEvent.ACTION_MOVE:
				if (pTouchAreaLocalY > 0 && pTouchAreaLocalX < WIDTH + 100) {
					Command.mThrottle = (int) pTouchAreaLocalY * 255 / HEIGHT;
					if (null != onThrottleChangeListener)
						onThrottleChangeListener
								.onThrottleChange(Command.mThrottle);
					if (period * 0 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 1) {
						this.setCurrentTileIndex(0);
					} else if (period * 1 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 2) {
						this.setCurrentTileIndex(1);
					} else if (period * 2 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 3) {
						this.setCurrentTileIndex(2);
					} else if (period * 3 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 4) {
						this.setCurrentTileIndex(3);
					} else if (period * 4 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 5) {
						this.setCurrentTileIndex(4);
					} else if (period * 5 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 6) {
						this.setCurrentTileIndex(5);
					} else if (period * 6 < pTouchAreaLocalY
							&& pTouchAreaLocalY < period * 7) {
						this.setCurrentTileIndex(6);
					} else if (period * 7 < pTouchAreaLocalY
							&& pTouchAreaLocalY < period * 8) {
						this.setCurrentTileIndex(7);
					} else if (period * 8 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 9) {
						this.setCurrentTileIndex(7);
					} else if (period * 9 < pTouchAreaLocalY
							&& pTouchAreaLocalY <= period * 10) {
						this.setCurrentTileIndex(8);
					} else if (period * 10 < pTouchAreaLocalY) {
						this.setCurrentTileIndex(9);
					}
					break;
				}
			}
			return true;
		} else {
			Toast.makeText(context,
					"Sorry please turn on the swither.",
					Toast.LENGTH_SHORT).show();
			return false;
		}
	}

	public interface OnThrottleChangeListener {
		public void onThrottleChange(int throttle);
	}

	public void setOnThrottleChangeListener(
			OnThrottleChangeListener onThrottleChangeListener) {
		this.onThrottleChangeListener = onThrottleChangeListener;
	}
}